package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.domain.flowerKnowledge.cqrs.FlowerKnowledgePageQuery;
import com.starlight.flower_server.infrastructure.entity.FlowerKnowledge;
import com.starlight.flower_server.infrastructure.mapper.FlowerKnowledgeMapper;
import com.starlight.flower_server.infrastructure.repository.FlowerKnowledgeRepository;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import com.starlight.flower_server.infrastructure.util.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author yfy2001
 * @date 2023/4/17 20:47
 */
@Repository
public class FlowerKnowledgeRepositoryImpl implements FlowerKnowledgeRepository {

    @Resource
    private FlowerKnowledgeMapper flowerKnowledgeMapper;

    @Override
    public FlowerKnowledge queryDetail(Long id) {
        return flowerKnowledgeMapper.selectById(id);
    }

    @Override
    public Integer logicDelete(Long id) {
        return flowerKnowledgeMapper.deleteById(id);
    }

    @Override
    public Integer insert(FlowerKnowledge flowerKnowledge) {
        String operator= CurrentUserInfoUtils.get("username");
        flowerKnowledge.setCreator(operator);
        flowerKnowledge.setModifier(operator);
        flowerKnowledge.setCreated(new Date());
        flowerKnowledge.setModified(new Date());
        flowerKnowledge.setIsDeleted(0);
        return flowerKnowledgeMapper.insert(flowerKnowledge);
    }

    @Override
    public Integer modify(FlowerKnowledge flowerKnowledge) {
        String modifier= CurrentUserInfoUtils.get("username");
        flowerKnowledge.setModifier(modifier);
        flowerKnowledge.setModified(new Date());
        return flowerKnowledgeMapper.updateById(flowerKnowledge);
    }

    @Override
    public IPage<FlowerKnowledge> queryPage(FlowerKnowledgePageQuery query) {
        QueryWrapper<FlowerKnowledge> queryWrapper= PageUtils.createOrderQuery(query);
        queryWrapper.lambda()
                .isNotNull(FlowerKnowledge::getId)
                .eq(Objects.nonNull(query.getId()),FlowerKnowledge::getId,query.getId())
                .like(Objects.nonNull(query.getName()),FlowerKnowledge::getName,query.getName())
                .like(Objects.nonNull(query.getOtherName()),FlowerKnowledge::getOtherName,query.getOtherName())
                .like(Objects.nonNull(query.getFlorescence()),FlowerKnowledge::getFlorescence,query.getFlorescence())
                .like(Objects.nonNull(query.getOrigin()),FlowerKnowledge::getOrigin,query.getOrigin());
        return flowerKnowledgeMapper.selectPage(PageUtils.startPage(query),queryWrapper);
    }

    @Override
    public List<FlowerKnowledge> queryAll() {
        return flowerKnowledgeMapper.selectList(new LambdaQueryWrapper<FlowerKnowledge>()
                .isNotNull(FlowerKnowledge::getId));
    }

    @Override
    public Long queryTotalCount() {
        return flowerKnowledgeMapper.selectCount(new LambdaQueryWrapper<FlowerKnowledge>()
                .isNotNull(FlowerKnowledge::getId));
    }

    @Override
    public List<FlowerKnowledge> queryRandom(Integer count) {
        return flowerKnowledgeMapper.selectList(new LambdaQueryWrapper<FlowerKnowledge>()
                .isNotNull(FlowerKnowledge::getId)
                .last(String.format("order by rand() limit %d",count)));
    }
}
